﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using mdryden.Core.Localization;

public static class TimezoneHelper
{
	private static double[] offsets;

	static TimezoneHelper()
	{
		offsets = new[]
			{
				-12,	// UTC−12:00
				-11,	// UTC−11:00
				-10,	// UTC−10:00
				-9.5,	// UTC−09:30
				-9,		// UTC−09:00
				-8,		// UTC−08:00
				-7,		// UTC−07:00
				-6,		// UTC−06:00
				-5,		// UTC−05:00
				-4.5,	// UTC−04:30
				-4,		// UTC−04:00
				-3.5,	// UTC−03:30
				-3,		// UTC−03:00
				-2,		// UTC−02:00
				-1,		// UTC−01:00
				 0,		// UTC±0
				 1,		// UTC+01:00
				 2,		// UTC+02:00
				 3,		// UTC+03:00
				 3.5,	// UTC+03:30
				 4,		// UTC+04:00
				 4.5,	// UTC+04:30
				 5,		// UTC+05:00
				 5.5,	// UTC+05:30
				 5.75,	// UTC+05:45
				 6,		// UTC+06:00
				 6.5,	// UTC+06:30
				 7,		// UTC+07:00
				 8,		// UTC+08:00
				 8.45,	// UTC+08:45
				 9,		// UTC+09:00
				 9.5,	// UTC+09:30
				 10,	// UTC+10:00
				 10.5,	// UTC+10:30
				 11,	// UTC+11:00
				 11.5,	// UTC+11:30
				 12,	// UTC+12:00
				 12.75,	// UTC+12:45
				 13,	// UTC+13:00
				 14,	// UTC+14:00
			};
	}

	public static IHtmlString GetTimezoneOffsetList(this HtmlHelper helper, string id, string name, double value)
	{
		const string openTag = "<select id='{0}' name='{1}'>";
		const string optionTag = "\t<option value='{0}' {2}>{1}</option>";
		const string closeTag = "</select>";

		var html = new StringBuilder();

		html.AppendLine(string.Format(openTag, id, name));

		foreach (var offset in offsets)
		{
			var text = LanguageLoader.GetWord("TimezoneOffset_" + offset);
			var selected = offset == value ? "selected='selected'" : "";
			html.AppendLine(string.Format(optionTag, offset, text, selected));
		}

		html.Append(closeTag);

		return helper.Raw(html.ToString());
	}

	public static DateTime AdjustedTime(this HtmlHelper helper, DateTime date)
	{
		var offset = helper.UserSettings().TimeOffset;

		return date.AddHours(offset);
	}
}
